package com.by.zhdoctor.algorithmsUtil;

import com.by.zhdoctor.domain.RuleRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 123 on 2017/4/7.
 */
public class MatchStrategy implements AbstractStrategy{

    private final Logger log = LoggerFactory.getLogger(MatchStrategy.class);


    private EntityManager em;

    public MatchStrategy(EntityManager em){
        this.em=em;
    }

    @Override
    public List<?> strategy(String doctorName,String bmName,List<String> list) {
        //相关的算法
        String hql = "select rrp from RuleRepository rrp  where rrp.doctorName = :owner and rrp.bingmingName = :bn";

        Query query = em.createQuery(hql);
        query.setParameter("owner",doctorName);
        query.setParameter("bn",bmName);
        List<RuleRepository> ruleByName=query.getResultList();
        log.debug(ruleByName.toString());
        List<RuleRepository> result = match(ruleByName,list);
        return  result;
    }

    private List<RuleRepository> match(List<RuleRepository> targetList, List<String> matchList){


        List<RuleRepository> result = new ArrayList<>();


                for (int j=0;j<targetList.size();j++)
                {

                    int pIndex=0;
                    Iterator it1 = matchList.iterator();
                    log.debug(targetList.get(j).getZhengxingList());
                    while(it1.hasNext()) {
                        String match=it1.next().toString();
                        log.debug(match);
                        if(targetList.get(j).getZhengxingList().contains(match)){
                            pIndex++;
                            if (pIndex==matchList.size()){
                                result.add(targetList.get(j));
                                pIndex=0;
                            }

                        }

                    }

                }






        return result;
    }


}
